package com.hundsun.documentripper.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hundsun.documentripper.domain.dto.BaseResponse;
import com.hundsun.documentripper.domain.pojo.User;
import com.hundsun.documentripper.domain.query.BaseQuery;
import com.hundsun.documentripper.error.ErrorCatcher;
import com.hundsun.documentripper.service.UserService;

/**
 * Note：
 *
 * @author ：mengjw
 * @description：用户控制层
 * @date ：Created in 2021/1/26
 */
@Api("用户")
@RequestMapping("/user")
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation("新建用户")
    @PostMapping("/")
    @ErrorCatcher(code = "1001")
    public BaseResponse<Void> insertUser(@RequestBody User user) {
        userService.insertUser(user);
        return new BaseResponse<Void>();
    }

    @ApiOperation("更新用户信息")
    @PutMapping("/")
    @ErrorCatcher(code = "1003")
    public BaseResponse<Void> updateUser(@RequestBody User user) {
        userService.updateUser(user);
        return new BaseResponse<Void>();
    }

    @ApiOperation("根据唯一标识获取用户对象")
    @GetMapping("/{uuid}")
    @ErrorCatcher(code = "1004")
    public BaseResponse<User> getUserById(@PathVariable("uuid") String uuid) {
        return new BaseResponse<User>(userService.getUserById(uuid));
    }

    @ApiOperation("分页获取用户列表")
    @PostMapping("/all")
    @ErrorCatcher(code = "1005")
    public BaseResponse<IPage<User>> getUserListByPage(@RequestBody BaseQuery baseQuery) {
        return new BaseResponse<IPage<User>>(userService.getUserListByPage(baseQuery));
    }

    @ApiOperation("删除用户")
    @DeleteMapping("/{uuid}")
    @ErrorCatcher(code = "1006")
    public BaseResponse<Void> deleteUser(@PathVariable("uuid") String uuid) {
        userService.deleteUser(uuid);
        return new BaseResponse<Void>();
    }

    @ApiOperation("登录")
    @PostMapping("/login")
    @ErrorCatcher(code = "1007")
    public BaseResponse<User> login(@RequestBody User user, HttpServletRequest request) {
        BaseResponse<User> loginResponse = userService.login(user);
        if (loginResponse.getReturnCode() == 0) {
            HttpSession session = request.getSession();
            session.setAttribute("userUuid", loginResponse.getResult().getUuid());
        }
        return loginResponse;
    }

    @ApiOperation("获取当前用户")
    @GetMapping("/current")
    @ErrorCatcher(code = "1009")
    public BaseResponse<User> getCurrentUser(HttpServletRequest request) {
        HttpSession session = request.getSession();
        return new BaseResponse<User>(userService.getUserById((String) session.getAttribute("userUuid")));
    }

    @ApiOperation("登出")
    @GetMapping("/logout")
    @ErrorCatcher(code = "1011")
    public BaseResponse<Void> login(HttpServletRequest request) {
        request.getSession().removeAttribute("userUuid");
        return new BaseResponse<Void>();
    }

}